package com.yyk.yykywd.ui.login;

import android.os.Handler;

import com.orhanobut.hawk.Hawk;
import com.yyk.yykywd.BusinessPeopleInfoKt;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.bean.BaseBean;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.ui.MainActivity;
import com.yyk.yykywd.util.Md5Util2;

import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class WelcomeActivity extends BaseActivity {

    private Handler handler = new Handler();

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initView() {
        hidetoolBar();

    }

    @Override
    public void initData() {
        initWelcome();

    }

    private void initWelcome() {
        String ISLOGFLAG = Hawk.get("ISLOGFLAG", "LoginActivity");
        //true 跳转到 主页面   false跳转到 首次安装界面
        if (ISLOGFLAG.equals("LoginActivity")) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    startAct(LoginActivity.class, true);
                }
            }, 2000);
        } else if (ISLOGFLAG.equals("MainActivity")) {
            Map<String, String> map = new TreeMap<>();
            map.put("phone", BusinessPeopleInfoKt.getPhone());
            map.put("sign", Md5Util2.createSign(map));
            ApiService.getInstance().api.postCheckPhone(map)
                    .subscribeOn(Schedulers.io())     //调度线程
                    .observeOn(AndroidSchedulers.mainThread())
                    .unsubscribeOn(Schedulers.io())   //取消订阅
                    .subscribe(new Observer<BaseBean>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(BaseBean info) {
                            if (info.getCode() == 303) {
                                BusinessPeopleInfoKt.deleteUserData();
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        startAct(LoginActivity.class, true);
                                    }
                                }, 2000);
                            } else {
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        startAct(MainActivity.class, true);
                                    }
                                }, 2000);
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            BusinessPeopleInfoKt.deleteUserData();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    startAct(LoginActivity.class, true);
                                }
                            }, 2000);
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }


    }
}
